package mastermind.gui.contents;

import mastermind.Application;
import mastermind.gui.controls.ButtonUI;
import mastermind.gui.controls.GroupUI;
import mastermind.gui.controls.LabelUI;
import mastermind.logic.Game;
import mastermind.logic.Game.GameType;
import mastermind.tools.Helper;
import mastermind.tools.LoaderHelper;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;

/**
 * Diese Klasse stellt das Willkommenfester dar und bietet grundlegende
 * Einstellm&ouml;glichkeiten an.
 */
public class WelcomeUI extends Composite
{
	private static WelcomeUI	_this;
	private GroupUI				_grpSettings;
	private LabelUI				_lblMastermind;
	private LabelUI				_lblAuthor;
	private LabelUI				_lblColors;
	private LabelUI				_lblHoles;
	private LabelUI				_lblGameType;
	private ButtonUI			_btnLoadGame;
	private ButtonUI			_btnStartGame;
	private ButtonUI			_btnExit;
	private Combo				_cmbColors;
	private Combo				_cmbHoles;
	private Combo				_cmbGameType;

	/**
	 * Erstellt ein neues WelcomeUI Objekt
	 * 
	 * @param parent
	 *            Eltern-Objekt, auf das das WelcomeUI Objekt plaziert wird
	 * @param style
	 *            Zus&auml;zliche Style Informationen
	 */
	public WelcomeUI(Composite parent, int style)
	{
		super(parent, style);
		_this = this;

		createContents();
	}

	/**
	 * Gibt die einzige Instanz dieser Klasse zur&uuml;ck.
	 * 
	 * @return Instanz dieser Klasse.
	 */
	public static WelcomeUI getInstance()
	{
		return _this;
	}

	/**
	 * Hier werden alle Elemente erstellt, die ben&ouml;tigt werden um das
	 * Objekt anzuzeigen<br>
	 * Diese Funktion wird nur einmal aufgerufen
	 */
	private void createContents()
	{
		this.setBackground(new Color(Display.getDefault(), new RGB(47, 141, 89)));
		this.setBackgroundMode(SWT.INHERIT_FORCE);

		_btnExit = new ButtonUI(this, SWT.NONE);
		_btnExit.setText("X");
		_btnExit.setSize(20, 20);
		_btnExit.setLocation(320, 0);
		_btnExit.setToolTipText("Spiel beenden");

		_lblMastermind = new LabelUI(this, SWT.NONE);
		_lblMastermind.setText("Mastermind");
		_lblMastermind.setAlignment(SWT.CENTER);
		_lblMastermind.setFont(LoaderHelper.getFont("Segoe UI", 18, SWT.BOLD));
		_lblMastermind.setSize(302, 25);
		_lblMastermind.setLocation((this.getSize().x / 2) - (_lblMastermind.getSize().x / 2), 10);

		_lblAuthor = new LabelUI(this, SWT.NONE);
		_lblAuthor.setText("\u00A9 PP-4   Bergen, Delen, Nusch, Okan");
		_lblAuthor.setAlignment(SWT.RIGHT);
		_lblAuthor.setForeground(new Color(Display.getDefault(), new RGB(30, 30, 30)));
		_lblAuthor.setFont(LoaderHelper.getFont("Segoe UI", 9, SWT.BOLD));
		_lblAuthor.setSize(302, 14);
		_lblAuthor.setLocation((this.getSize().x) - _lblAuthor.getSize().x - 5, this.getSize().y - _lblAuthor.getSize().y - 5);

		_btnStartGame = new ButtonUI(this, SWT.NONE);
		_btnStartGame.setText("Spiel starten");
		_btnStartGame.setSize(125, 35);
		_btnStartGame.setLocation(
				(this.getSize().x / 2) + 5,
				_lblMastermind.getSize().y + _lblMastermind.getLocation().y + 15
				);

		_btnLoadGame = new ButtonUI(this, SWT.NONE);
		_btnLoadGame.setText("Spiel laden");
		_btnLoadGame.setSize(125, 35);

		_btnLoadGame.setLocation(
				(this.getSize().x / 2) - (_btnLoadGame.getSize().x) - 5,
				_lblMastermind.getSize().y + _lblMastermind.getLocation().y + 15
				);

		_grpSettings = new GroupUI(this, SWT.NONE);
		_grpSettings.setFont(LoaderHelper.getFont("Segoe UI", 9, SWT.BOLD));
		_grpSettings.setText("Spieleinstellungen");
		_grpSettings.setSize(242, 110);
		_grpSettings.setLocation(
				(this.getSize().x / 2) - (_grpSettings.getSize().x / 2),
				_btnLoadGame.getSize().y + _btnLoadGame.getLocation().y + 15
				);

		_lblGameType = new LabelUI(_grpSettings, SWT.NONE);
		_lblGameType.setText("Spieltyp");
		_lblGameType.setBounds(10, 81, 55, 15);
		_lblColors = new LabelUI(_grpSettings, SWT.NONE);
		_lblColors.setText("Farben");
		_lblColors.setBounds(10, 23, 91, 15);
		_lblHoles = new LabelUI(_grpSettings, SWT.NONE);
		_lblHoles.setText("L\u00F6cher");
		_lblHoles.setBounds(10, 52, 91, 15);

		// Combo
		_cmbColors = new Combo(_grpSettings, SWT.READ_ONLY | SWT.FLAT);
		int defIndex = 0;
		String[] items = new String[Game.MAX_COLOR - Game.MIN_COLOR + 1];
		for (int i = 0; i < Game.MAX_COLOR - Game.MIN_COLOR + 1; i++)
		{
			items[i] = "" + (Game.MIN_COLOR + i);
			if (Game.MIN_COLOR + i == 6)
			{
				defIndex = i;
			}
		}
		_cmbColors.setItems(items);
		_cmbColors.setBounds(107, 20, 55, 23);
		_cmbColors.select(defIndex);
		_cmbHoles = new Combo(_grpSettings, SWT.READ_ONLY);
		items = new String[Game.MAX_HOLES - Game.MIN_HOLES + 1];
		for (int i = 0; i < Game.MAX_HOLES - Game.MIN_HOLES + 1; i++)
		{
			items[i] = "" + (Game.MIN_HOLES + i);
			if (Game.MIN_HOLES + i == 4)
			{
				defIndex = i;
			}
		}
		_cmbHoles.setItems(items);
		_cmbHoles.setBounds(107, 49, 55, 23);
		_cmbHoles.select(defIndex);
		_cmbGameType = new Combo(_grpSettings, SWT.READ_ONLY);
		_cmbGameType.setItems(new String[] { "Ich rate", "Computer r\u00E4t", "2 Spieler" });
		_cmbGameType.setBounds(107, 78, 125, 23);
		_cmbGameType.select(0);

		// Events
		_btnExit.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e)
			{
				Application.doExit();
			}
		});

		_btnLoadGame.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e)
			{
				Helper.loadGame();
			}
		});

		_btnStartGame.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e)
			{
				btnStartNewGameClick(e);
			}
		});
	}

	/**
	 * Gibt die gr&ouml;&szlig;e zur&uuml;ck
	 */
	public Point getSize()
	{
		return new Point(340, 240);
	}

	/**
	 * Der Button Spiel starten wurde gedr&uuml;ckt
	 * 
	 * @param e
	 *            Event des Click-Ereignisses
	 */
	private void btnStartNewGameClick(SelectionEvent e)
	{
		try
		{
			GameType gameType;

			switch (_cmbGameType.getSelectionIndex())
			{
			case 0:
				gameType = GameType.USER;
				break;
			case 1:
				gameType = GameType.PC;
				break;
			case 2:
				gameType = GameType.TWO_PLAYER;
				break;
			default:
				return;
			}
			GameUI.reset();
			Game.init(Byte.parseByte(_cmbColors.getText()), Byte.parseByte(_cmbHoles.getText()), gameType);
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
